package com.ishell.generate;

import com.ishell.generate.pattern.strategy.*;
import com.ishell.generate.util.FileUtil;
import com.ishell.generate.util.PropertyUtil;
import com.ishell.generate.util.Streams;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

/**
 * @author: crazier.huang
 * @project: generate
 * @date: 2018/6/8
 * @desc:
 */
public class Generate {
    private final static Logger logger = LoggerFactory.getLogger(Generate.class);



    public String generateCode(String packageName, TableDesc table, String templatePath) throws IOException {
        VelocityContext context = new VelocityContext();
        context.put("table", table);
        context.put("packageName", packageName);
        context.put("time",new Date());
        StringWriter writer = new StringWriter();
        // todo

        String template = new String(Streams.readBytes(this.getClass().getResourceAsStream(templatePath)),
                Charset.forName("utf8"));
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty("runtime.references.strict", false);
        engine.init();
        engine.evaluate(context, writer, "generator", template);
        return writer.toString();

    }

    public void generate(String packageName,TableDesc table ,String templatePath, File file, boolean force)
            throws IOException {
        if (file.exists() && !force) {
            logger.debug("file " + file + " exists, skipped");
            return;
        }

        String code = generateCode(packageName, table,templatePath);
        file.getParentFile().mkdirs();
        FileUtil.rewirte(file, code);

    }

    public static void main(String[] args) throws Exception {


        String packageName = PropertyUtil.getValue("prex.packageName");
        String fileName = StringUtils.replace(packageName,".","/");
        String dbType = PropertyUtil.getValue("db.type");

        GenTableStrategy tableStrategy = null;
        GenColumnStrategy columnStrategy = null;
        if(StringUtils.equals(dbType,"mysql")){
            tableStrategy = new GenTableMysqStrategy();
            columnStrategy = new GenColunmnMysqlStrategy();
        }
        if(StringUtils.equals(dbType,"oracle")){

            tableStrategy = new GenTableOracleStrategy();
            columnStrategy = new GenColunmnOrcaleStrategy();
        }
        List<TableDesc> tableDescList = tableStrategy.genTable();
        tableDescList = columnStrategy.genColumn(tableDescList);


        for (TableDesc table : tableDescList) {
            String modelName = PropertyUtil.getValue("prex.projectPath")+fileName+"/model/"+table.getClassName()+".java";
            String repoName = PropertyUtil.getValue("prex.projectPath")+fileName+"/repository/"+table.getClassName()+"Repository.java";
            String serviceName = PropertyUtil.getValue("prex.projectPath")+fileName+"/service/"+table.getClassName()+"Service.java";
            String serviceImplName = PropertyUtil.getValue("prex.projectPath")+fileName+"/service/impl/"+table.getClassName()+"ServiceImpl.java";
            Generate gen = new Generate();
            gen.generate(packageName,table,"/template/model.vm",new File(modelName),true);
            gen.generate(packageName,table, "/template/repo.vm",new File(repoName),true);
            gen.generate(packageName,table, "/template/service.vm",new File(serviceName),true);
            gen.generate(packageName,table, "/template/serviceImpl.vm",new File(serviceImplName),true);
        }



    }
}
